const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const merge = require('webpack-merge');
const webpackBaseConfig = require('./webpack.base.config.js');
// const fs = require('fs');
const path = require('path');

const VueLoaderPlugin = require('vue-loader/lib/plugin');

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

// const ENV_JS = path.resolve(__dirname, './src/config/env.js');
// fs.open(ENV_JS, 'w', (err, fd) => {
// 	const buf = 'export default "production";';
// 	/* eslint-disable-next-line */
// 	fs.write(fd, buf, 0, buf.length, 0, (err, written, buffer) => {});
// 	fs.close(fd, () => {});
// });

module.exports = merge(webpackBaseConfig, {
	mode: 'production',
	output: {
		publicPath: '/web-admin/dist/',
		filename: '[name].[chunkhash].js',
		chunkFilename: '[name].[chunkhash].chunk.js',
	},
	plugins: [
		new VueLoaderPlugin(),

		// @link https://github.com/webpack-contrib/mini-css-extract-plugin
		new MiniCssExtractPlugin({
			filename: 'css/[name].[hash].css',
			chunkFilename: 'css/[name].[hash].css',
		}),
		/**
         * @link https://github.com/chenchunyong/webpack-dllPlugin 关于dll使用的github地址
         * @link https://doc.webpack-china.org/plugins/dll-plugin/ webpack官方文档
         * @link https://segmentfault.com/a/1190000005969643 例子使用
         */
		new webpack.DllReferencePlugin({
			context: __dirname,
			/* eslint-disable-next-line import/no-dynamic-require */
			manifest: require(path.join(__dirname, '.', 'manifest.json')),
		}),

		new webpack.DefinePlugin({
			'process.env': {
				NODE_ENV: '"production"',
			},
		}),

		/**
         * @link https://github.com/jantimon/html-webpack-plugin#configuration
         */
		new HtmlWebpackPlugin({
			filename: '../index.html',
			template: './src/template/index.ejs',
			inject: true,
		}),

		/**
         * @link https://www.npmjs.com/package/html-webpack-include-assets-plugin
         */
		new HtmlWebpackIncludeAssetsPlugin({
			assets: ['/static/vendors.web-admin.js'],
			publicPath: false,
			append: false,
			hash: true,
		}),

		// @link https://github.com/NMFR/optimize-css-assets-webpack-plugin压缩css并且保留下前缀
		new OptimizeCSSAssetsPlugin({
			assetNameRegExp: /\.css\.*(?!.*map)/g, // 注意不要写成 /\.css$/g
			cssProcessor: require('cssnano'),
			cssProcessorOptions: {
				preset: ['default', { discardComments: { removeAll: true } }],
				// discardComments: { removeAll: true },
				// 避免 cssnano 重新计算 z-index
				safe: true,
				// cssnano 集成了autoprefixer的功能
				// 会使用到autoprefixer进行无关前缀的清理
				// 关闭autoprefixer功能
				// 使用postcss的autoprefixer功能
				// autoprefixer: false,
				autoprefixer: { remove: false },
			},
			canPrint: true,
		}),

		/**
         * watch 编译时忽略这个路径或文件（一定是绝对路径）
         */
		// new webpack.WatchIgnorePlugin([
		// 	// ENV_JS,
		// ]),
	],
});
